<?php

namespace App\Http\Middleware\WechatServiceAccount;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

class WechatAuth
{
    public function handle(Request $request, Closure $next)
    {
        Log::info('WechatAuth Middleware Request:', [
            'url' => $request->fullUrl(),
            'method' => $request->method(),
            'ip' => $request->ip(),
            'user_agent' => $request->userAgent(),
            'session_id' => $request->session()->getId(),
            'input_data' => $request->all()
        ]);

        // 检查微信认证状态
        if (Auth::guard('wechat')->check()) {
            return $next($request);
        }

        // 保存当前完整URL（包括查询参数）作为意图访问的URL
        session()->put('url.intended', $request->fullUrl());
        
        // 未认证则重定向到微信登录页面
        return redirect()->route('wechat.service.account.login');
    }
}